package server.qiang.Process;

import common.harvin.potocol.Request;
import common.harvin.potocol.Response;
import server.qiang.Thread.OutputThread;
import server.qiang.Thread.OutputThreadMap;
import server.qiang.database.IpMap;
import server.qiang.database.IpObject;
import server.qiang.database.database;

import java.sql.SQLException;

public class GetIp {
	private Request request;
	private IpMap ipmap;
	private IpObject ipobject;
	private OutputThreadMap map;

	public Response getip(Request request,OutputThreadMap map, IpMap ipmap) {
		this.request = request;
		this.ipmap = ipmap;
		this.map=map;
		System.out.println("command:" + request.getCommand());
		System.out.println("sender:" + request.getSender());
		System.out.println("receiver:"+request.getReceiver());
		System.out.println("send_Date:" + request.getSend_Date());


		database t = new database();

		Response response = new Response();
		response.setFor_command(request.getCommand());
		response.setSend_Date(request.getSend_Date());

		try {
			if (t.IsUser(request.getReceiver())) {
				OutputThread out = map.getById(request.getReceiver());
				if(out!=null)
				{
					//out.setMessage(response);
				    ipobject = ipmap.getById(request.getReceiver());
				    if (ipobject != null) {
					System.out.println(ipobject.getIp());
					System.out.println(ipobject.getPort());

					response.addData("ip", ipobject.getIp());
					response.addData("port", String.valueOf(ipobject.getPort()));

					System.out.println("get ip sucessful");
					response.setResult(true);
				     } else {
					System.out.println("对方没有上传ip");
					response.addData("response", "对方没有上传ip");
				            }
				
				}else
				{
					System.out.println("对方ip不在");
				}
				
			} else {
				System.out.println("对方非用户，get ip fail");
				response.addData("response", "对方非用户，get ip fail");
			}

		} catch (SQLException e) {

		}


		return response;
	}
}
